package com.atguigu.controller;

import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.RedisConstant;
import com.atguigu.pojo.Member;
import com.atguigu.pojo.Setmeal;
import com.atguigu.service.SetmealService;
import com.atguigu.util.QiniuUtils;
import com.atguigu.vo.PageResult;
import com.atguigu.vo.QueryPageBean;
import com.atguigu.vo.Result;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/Setmeal")
public class SetmealController {

    @Reference
    SetmealService setmealService;

    @Autowired
    JedisPool jedisPool;

    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
            PageResult pageResult = setmealService.fainPage(queryPageBean.getQueryString(),
                    queryPageBean.getCurrentPage(),
                    queryPageBean.getPageSize());

            return pageResult;
    }

    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile imgFile){
        try {
            byte[] bytes = imgFile.getBytes();
            String filename = imgFile.getOriginalFilename();
            String newFilename = UUID.randomUUID().toString()+"_"+filename;
            QiniuUtils.upload2Qiniu(bytes,newFilename);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,newFilename);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,newFilename);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @PostMapping("/add")
    public Result add(Integer[] travelgroupIds,@RequestBody Setmeal setmeal){
        try {
            setmealService.add(travelgroupIds,setmeal);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @GetMapping("/get")
    public Result get(Integer id){
        try {
            Setmeal setmeal = setmealService.get(id);
            return new Result(true,"查看套餐游成功",setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查看套餐游失败");
        }
    }

    @GetMapping("/getSetmealIdByTravelgroupId")
    public Result getSetmealIdByTravelgroupId(Integer id){
        try {
            Integer[] TravelgroupId = setmealService.getSetmealIdByTravelgroupId(id);
            return new Result(true,"复选框回显成功",TravelgroupId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"复选框回显失败");
        }
    }

    @PostMapping("/update")
    public Result update(Integer[] travelgroupIds,@RequestBody Setmeal setmeal){
        try {
            setmealService.update(travelgroupIds,setmeal);
            return new Result(true,"套餐编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"套餐编辑失败");
        }
    }

    @GetMapping("/deleteSetmeal")
    public Result deleteSetmeal(Integer id){
        try {
            setmealService.deleteSetmeal(id);
            return new Result(true,"删除成功");
        } catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false,"有关联属性，请先解除");
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }

}
